import 'package:flutter/material.dart';
import '../../api/home.dart';
import 'components/house_item.dart';

class HouseList extends StatefulWidget {
  const HouseList({super.key});

  @override
  State<HouseList> createState() => _HouseListState();
}

class _HouseListState extends State<HouseList> {
  List list = []; // 定义数组

  void getHouseList() async {
    list = await getHouseListAPI();
    setState(() {});
  }

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    getHouseList();
  }

// 抽取列表
  ListView getHouseListWidget() {
    return ListView.separated(
      itemBuilder: (BuildContext context, int index) {
        return HouseItem(itemMap: list[index]);
      },
      separatorBuilder: (BuildContext context, int index) {
        return Container(
          height: 10,
          color: const Color.fromARGB(255, 241, 238, 238),
        );
      },
      itemCount: list.length,
    );
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: const Color.fromARGB(255, 241, 238, 238),
      appBar: AppBar(
        title: const Text('我的房屋'),
        backgroundColor: Colors.transparent, // 可以根据需要调整 AppBar 背景色
        elevation: 0, // 移除阴影
        foregroundColor: Colors.black, // 设置标题和图标颜色
        centerTitle: true,
      ),
      body: Stack(
        children: [
          Padding(
            padding: const EdgeInsets.only(bottom: 80, left: 10, right: 10),
            // child: ListView.separated( // 移除旧的 ListView
            //   itemBuilder: (BuildContext context, int index) {
            //     return const HouseItem();
            //   },
            //   separatorBuilder: (BuildContext context, int index) {
            //     return Container(
            //       height: 10,
            //       color: const Color.fromARGB(255, 241, 238, 238),
            //     );
            //   },
            //   itemCount: 5,
            // ),
            child: getHouseListWidget(), // 调用新的方法
          ),
          Positioned(
            bottom: 0,
            left: 0,
            right: 0,
            child: Container(
              padding: const EdgeInsets.all(10),
              color: Colors.transparent, // 背景设为透明，避免遮挡列表底部
              child: Row(
                children: [
                  Expanded(
                    child: ElevatedButton(
                      style: ElevatedButton.styleFrom(
                        padding: EdgeInsets.zero, // 移除默认内边距
                        shape: RoundedRectangleBorder(
                          // 设置圆角
                          borderRadius: BorderRadius.circular(8),
                        ),
                        // 可以设置背景色等样式
                        // backgroundColor: Theme.of(context).primaryColor,
                      ),
                      onPressed: () {
                        // TODO: 实现添加房屋的逻辑
                        print('添加房屋按钮被点击');
                        Navigator.pushNamed(context, '/add_House');
                      },
                      child: const Padding(
                        // 添加内边距
                        padding: EdgeInsets.symmetric(vertical: 12.0),
                        child: Column(
                          mainAxisSize: MainAxisSize.min, // 让 Column 包裹内容
                          children: [
                            Icon(Icons.add),
                            SizedBox(height: 5), // 图标和文字间距
                            Text('添加房屋'),
                          ],
                        ),
                      ),
                    ),
                  )
                ],
              ),
            ),
          )
        ],
      ),
    );
  }
}
